<template>
  <div class="app-container">
    <div class="head-container">
      <!-- 搜索 -->
      <el-input v-model="listQuery.Filter" clearable size="small" placeholder="搜索..." style="width: 200px;"
        class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button class="filter-item" size="mini" type="success" icon="el-icon-search"
        @click="handleFilter">搜索</el-button>
      <el-button class="filter-item" size="mini" type="primary" icon="el-icon-plus"
        @click="handleCreate">新增</el-button>
      <el-button class="filter-item" size="mini" type="success" icon="el-icon-edit"
        @click="handleUpdate()">修改</el-button>
      <el-button slot="reference" class="filter-item" type="danger" icon="el-icon-delete" size="mini"
        @click="handleDelete()">删除</el-button>
    </div>
    <el-dialog :visible.sync="dialogFormVisible" :close-on-click-modal="false" @close="cancel()"
      :title="formTitle">
      <el-form ref="form" :model="form" :rules="rules" size="medium" label-width="100px">
        <el-form-item label="商品编码" prop="goodNo">
          <el-input v-model="form.goodNo" placeholder="请输入商品编码" clearable :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="商品名称" prop="goodName">
          <el-input v-model="form.goodName" placeholder="请输入商品名称" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="重量" prop="weight">
          <el-input v-model="form.weight" placeholder="请输入重量" clearable :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="参考进价" prop="referenceprice">
          <el-input v-model="form.referenceprice" placeholder="请输入参考进价" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label=" 宽" prop="width">
          <el-input v-model="form.width" placeholder="请输入 宽" clearable :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="条码" prop="barcode">
          <el-input v-model="form.barcode" placeholder="请输入条码" clearable :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" clearable :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="吊牌价" prop="hangTagPrice">
          <el-input v-model="form.hangTagPrice" placeholder="请输入吊牌价" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="辅助条码" prop="auxiliarybarcode">
          <el-input v-model="form.auxiliarybarcode" placeholder="请输入辅助条码" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="简称" prop="abbreviation">
          <el-input v-model="form.abbreviation" placeholder="请输入简称" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="长" prop="length">
          <el-input v-model="form.length" placeholder="请输入长" clearable :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="货号" prop="itemNo">
          <el-input v-model="form.itemNo" placeholder="请输入货号" clearable :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入单位" clearable :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="高" prop="height">
          <el-input v-model="form.height" placeholder="请输入高" clearable :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="商品等级" prop="grade">
          <el-input v-model="form.grade" placeholder="请输入商品等级" clearable :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="体积" prop="volume">
          <el-input v-model="form.volume" placeholder="请输入体积" clearable :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="分类" prop="classification">
          <el-input v-model="form.classification" placeholder="请输入分类" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="品牌" prop="brandNo">
          <el-input v-model="form.brandNo" placeholder="请输入品牌" clearable :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="类型" prop="goodType">
          <el-input v-model="form.goodType" placeholder="请输入类型" clearable :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="长度单位" prop="lengthUnit">
          <el-input v-model="form.lengthUnit" placeholder="请输入长度单位" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="体积单位" prop="volumeUnit">
          <el-input v-model="form.volumeUnit" placeholder="请输入体积单位" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="重量单位" prop="weightUnit">
          <el-input v-model="form.weightUnit" placeholder="请输入重量单位" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="是否保价" prop="insuredValue">
          <el-input v-model="form.insuredValue" placeholder="请输入是否保价" clearable
            :style="{width: '100%'}"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button size="small" type="text" @click="cancel">取消</el-button>
        <el-button size="small" v-loading="formLoading" type="primary" @click="save">确认</el-button>
      </div>
    </el-dialog>
    <el-table ref="multipleTable" v-loading="listLoading" :data="list" size="small" style="width: 90%;"
      @sort-change="sortChange" @selection-change="handleSelectionChange" @row-click="handleRowClick">
      <el-table-column type="selection" width="44px"></el-table-column>
      <el-table-column label="商品编码" prop="goodNo" align="center" />
      <el-table-column label="商品名称" prop="goodName" align="center" />
      <el-table-column label="重量" prop="weight" align="center" />
      <el-table-column label="参考进价" prop="referenceprice" align="center" />
      <el-table-column label=" 宽" prop="width" align="center" />
      <el-table-column label="条码" prop="barcode" align="center" />
      <el-table-column label="备注" prop="remark" align="center" />
      <el-table-column label="吊牌价" prop="hangTagPrice" align="center" />
      <el-table-column label="辅助条码" prop="auxiliarybarcode" align="center" />
      <el-table-column label="简称" prop="abbreviation" align="center" />
      <el-table-column label="长" prop="length" align="center" />
      <el-table-column label="货号" prop="itemNo" align="center" />
      <el-table-column label="单位" prop="unit" align="center" />
      <el-table-column label="高" prop="height" align="center" />
      <el-table-column label="商品等级" prop="grade" align="center" />
      <el-table-column label="体积" prop="volume" align="center" />
      <el-table-column label="分类" prop="classification" align="center" />
      <el-table-column label="品牌" prop="brandNo" align="center" />
      <el-table-column label="类型" prop="goodType" align="center" />
      <el-table-column label="长度单位" prop="lengthUnit" align="center" />
      <el-table-column label="体积单位" prop="volumeUnit" align="center" />
      <el-table-column label="重量单位" prop="weightUnit" align="center" />
      <el-table-column label="是否保价" prop="insuredValue" align="center" />
      <el-table-column label="操作" align="center">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)" icon="el-icon-edit" />
          <el-button type="danger" size="mini" @click="handleDelete(row)" icon="el-icon-delete" />
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="totalCount>0" :total="totalCount" :page.sync="page"
      :limit.sync="listQuery.MaxResultCount" @pagination="getList" />
  </div>
</template>
<script>
import Pagination from "@/components/Pagination";
import permission from "@/directive/permission/index.js";
const defaultForm = {
  id: null,
  goodNo: null,
  goodName: null,
  weight: null,
  referenceprice: null,
  width: null,
  barcode: null,
  remark: null,
  hangTagPrice: null,
  auxiliarybarcode: null,
  abbreviation: null,
  length: null,
  itemNo: null,
  unit: null,
  height: null,
  grade: null,
  volume: null,
  classification: null,
  brandNo: null,
  goodType: null,
  lengthUnit: null,
  volumeUnit: null,
  weightUnit: null,
  insuredValue: null,
}
export default {
  name: 'Good',
  components: {
    Pagination
  },
  directives: {
    permission
  },
  props: [],
  data() {
    return {
      rules: {
        goodNo: [{
          required: true,
          message: '请输入商品编码',
          trigger: 'blur'
        }],
        goodName: [{
          required: true,
          message: '请输入商品名称',
          trigger: 'blur'
        }],
        weight: [{
          required: true,
          message: '请输入重量',
          trigger: 'blur'
        }],
        referenceprice: [{
          required: true,
          message: '请输入参考进价',
          trigger: 'blur'
        }],
        width: [{
          required: true,
          message: '请输入 宽',
          trigger: 'blur'
        }],
        barcode: [{
          required: true,
          message: '请输入条码',
          trigger: 'blur'
        }],
        remark: [{
          required: true,
          message: '请输入备注',
          trigger: 'blur'
        }],
        hangTagPrice: [{
          required: true,
          message: '请输入吊牌价',
          trigger: 'blur'
        }],
        auxiliarybarcode: [{
          required: true,
          message: '请输入辅助条码',
          trigger: 'blur'
        }],
        abbreviation: [{
          required: true,
          message: '请输入简称',
          trigger: 'blur'
        }],
        length: [{
          required: true,
          message: '请输入长',
          trigger: 'blur'
        }],
        itemNo: [{
          required: true,
          message: '请输入货号',
          trigger: 'blur'
        }],
        unit: [{
          required: true,
          message: '请输入单位',
          trigger: 'blur'
        }],
        height: [{
          required: true,
          message: '请输入高',
          trigger: 'blur'
        }],
        grade: [{
          required: true,
          message: '请输入商品等级',
          trigger: 'blur'
        }],
        volume: [{
          required: true,
          message: '请输入体积',
          trigger: 'blur'
        }],
        classification: [{
          required: true,
          message: '请输入分类',
          trigger: 'blur'
        }],
        brandNo: [{
          required: true,
          message: '请输入品牌',
          trigger: 'blur'
        }],
        goodType: [{
          required: true,
          message: '请输入类型',
          trigger: 'blur'
        }],
        lengthUnit: [{
          required: true,
          message: '请输入长度单位',
          trigger: 'blur'
        }],
        volumeUnit: [{
          required: true,
          message: '请输入体积单位',
          trigger: 'blur'
        }],
        weightUnit: [{
          required: true,
          message: '请输入重量单位',
          trigger: 'blur'
        }],
        insuredValue: [{
          required: true,
          message: '请输入是否保价',
          trigger: 'blur'
        }],
      },
      form: Object.assign({}, defaultForm),
      list: null,
      totalCount: 0,
      listLoading: true,
      formLoading: false,
      listQuery: {
        Filter: '',
        Sorting: '',
        SkipCount: 0,
        MaxResultCount: 10
      },
      page: 1,
      dialogFormVisible: false,
      multipleSelection: [],
      formTitle: '',
      isEdit: false,
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getList()
  },
  mounted() {},
  methods: {
    getList() {
      this.listLoading = true;
      this.listQuery.SkipCount = (this.page - 1) * this.listQuery.MaxResultCount;
      this.$axios.gets('/api/business/Good', this.listQuery).then(response => {
        this.list = response.items;
        this.totalCount = response.totalCount;
        this.listLoading = false;
      });
    },
    fetchData(id) {
      this.$axios.gets('/api/business/Good/' + id).then(response => {
        this.form = response;
      });
    },
    handleFilter() {
      this.page = 1;
      this.getList();
    },
    handleCreate() {
      this.formTitle = '新增新商品表';
      this.isEdit = false;
      this.dialogFormVisible = true;
    },
    handleDelete(row) {
      var params = [];
      let alert = '';
      if (row) {
        params.push(row.id);
        alert = row.name;
      }
      else {
        if (this.multipleSelection.length === 0) {
          this.$message({
            message: '未选择',
            type: 'warning'
          });
          return;
        }
        this.multipleSelection.forEach(element => {
          let id = element.id;
          params.push(id);
        });
        alert = '选中项';
      }
      this.$confirm('是否删除' + alert + '?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$axios.posts('/api/business/Good/delete', params).then(response => {
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          });
          this.getList();
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    handleUpdate(row) {
      this.formTitle = '修改新商品表';
      this.isEdit = true;
      if (row) {
        this.fetchData(row.id);
        this.dialogFormVisible = true;
      }
      else {
        if (this.multipleSelection.length != 1) {
          this.$message({
            message: '编辑必须选择单行',
            type: 'warning'
          });
          return;
        }
        else {
          this.fetchData(this.multipleSelection[0].id);
          this.dialogFormVisible = true;
        }
      }
    },
    save() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.formLoading = true;
          this.form.roleNames = this.checkedRole;
          if (this.isEdit) {
            this.$axios.posts('/api/business/Good/data-post', this.form).then(response => {
              this.formLoading = false;
              this.$notify({
                title: '成功',
                message: '更新成功',
                type: 'success',
                duration: 2000
              });
              this.dialogFormVisible = false;
              this.getList();
            }).catch(() => {
              this.formLoading = false;
            });
          }
          else {
            this.$axios.posts('/api/business/Good/data-post', this.form).then(response => {
              this.formLoading = false;
              this.$notify({
                title: '成功',
                message: '新增成功',
                type: 'success',
                duration: 2000
              });
              this.dialogFormVisible = false;
              this.getList();
            }).catch(() => {
              this.formLoading = false;
            });
          }
        }
      });
    },
    sortChange(data) {
      const {
        prop,
        order
      } = data;
      if (!prop || !order) {
        this.handleFilter();
        return;
      }
      this.listQuery.Sorting = prop + ' ' + order;
      this.handleFilter();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleRowClick(row, column, event) {
      this.$refs.multipleTable.clearSelection();
      this.$refs.multipleTable.toggleRowSelection(row);
    },
    cancel() {
      this.form = Object.assign({}, defaultForm);
      this.dialogFormVisible = false;
      this.$refs.form.clearValidate();
    },
  }
}

</script>
<style>
</style>
